﻿using System;
using Quail.DbLogger;

namespace Quail
{
    /// <summary>
    /// Basic Test shell that provides simple HTTP GET/POST usage - no Selenium
    /// </summary>
    public class BasicTestShell : TestShell<BasicWebBrowser, BasicWebPage>, IQuailEnvironment
    {
        private readonly string _environmentName, _startupUrl;

        /// <summary>
        /// Creates a new instance of the BasicTestShell object
        /// </summary>
        /// <param name="testname">the name of the test</param>
        /// <param name="owninginstance">The object that is creating the BasicTestShell (for logging purposes)</param>
        /// <param name="environmentname">Name for the environment</param>
        /// <param name="startupurl">The startup URL for the environment</param>
        public BasicTestShell(string testname, object owninginstance, string environmentname, string startupurl)
            : base(testname, owninginstance)
        {
            _environmentName = environmentname;
            _startupUrl = startupurl;
        }

        public string GetEnvironmentName()
        {
            return _environmentName;
        }

        public override ITestShell CreateNewTestShell(string testName)
        {
            return new BasicTestShell(testName ?? TestName, this, _environmentName, _startupUrl);
        }

        public override string GetStartupUrl()
        {
            return _startupUrl;
        }

        /// <summary>
        /// Starts a new Atlas test shell
        /// </summary>
        /// <param name="testname">the name of the test</param>
        /// <param name="owninginstance">The object that is creating the BasicTestShell (for logging purposes)</param>
        /// <param name="environmentname">Name for the environment</param>
        /// <param name="startupurl">The startup URL for the environment</param>
        /// <returns>A new BasicTestShell</returns>
        public static BasicTestShell Start(string testname, object owninginstance, string environmentname, string startupurl)
        {
            var shell = new BasicTestShell(testname, owninginstance, environmentname, startupurl);
            try
            {
                shell.Start();
                return shell;
            }
            catch (Exception)
            {
                shell.Dispose();
                throw;
            }
        }

        protected override TestSession CreateNewTestSession()
        {
            return new BasicTestSession(this, TestName);
        }


        protected override void AddLoggerFactories()
        {
            // maybe add code here to turn off DB logging;  if (SAJRepositorySettings.Default.LogToDatabase)
            TestSession.RootTask.Logger.TaskFactories.Add(new QuailDbLogger(AssemblyName, ClassName, this));
        }
    }
}
